package com.chonglie;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class AlertDialogActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alert_dialog);
	}

	public void showAlertDialog(View view) {
		showDialog("TT", R.drawable.ic_launcher, "mm",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						setTitle("aa");
					}
				}, new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						setTitle("bb");
					}
				});
	}

	public void showAlertDialogWithEditText(View view) {
		LayoutInflater inflater = LayoutInflater.from(this);

		final View textEntryView = inflater.inflate(R.layout.alert_dialog_text,
				null);
		final EditText edtInput = (EditText) textEntryView
				.findViewById(R.id.alertdialog_edittext);
		showDialog("TT", R.drawable.ic_launcher, textEntryView,
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						setTitle(edtInput.getText());
					}
				}, new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						setTitle("");
					}
				});
	}

	private void showDialog(String title, int iconId, String message,
			OnClickListener confirmListener, OnClickListener cancelListener) {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setIcon(iconId);

		builder.setMessage(message);

		builder.setPositiveButton(R.string.COMMON_BTN_CONFIRM, confirmListener);
		builder.setNegativeButton(R.string.COMMON_BTN_CANCEL, cancelListener);

		builder.show();

	}

	private void showDialog(String title, int iconId, View view,
			OnClickListener confirmListener, OnClickListener cancelListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setIcon(iconId);

		builder.setView(view);

		builder.setPositiveButton(R.string.COMMON_BTN_CONFIRM, confirmListener);
		builder.setNegativeButton(R.string.COMMON_BTN_CANCEL, cancelListener);

		builder.show();
	}
}
